package com.lcc.lynx.common.utils;

import com.lcc.lynx.common.exception.ParamsException;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * 参数校验工具类
 */
public class StrUtil extends cn.hutool.core.util.StrUtil {

    public static void isNotBlank(String msg, Object... objs) {
        if (isEmpty(objs)) {
            throw new ParamsException(msg);
        }
        for (Object o : objs) {
            if (isEmpty(o)) {
                throw new ParamsException(msg);
            }
        }
    }

    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        } else if (o instanceof String) {
            return ((String) o).trim().length() == 0;
        } else if (o instanceof Collection) {
            return ((Collection<?>) o).size() == 0;
        } else if (o.getClass().isArray()) {
            return Array.getLength(o) == 0;
        } else if (o instanceof Map) {
            return ((Map<?, ?>) o).size() == 0;
        }
        return false;
    }

    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    public static String val(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static String val(Object obj, String def) {
        return obj == null ? def : obj.toString();
    }

    public static boolean isAllEmpty(Object[] args) {
        for (Object arg : args) {
            if (isNotEmpty(arg)) {
                return false;
            }
        }
        return true;
    }
}
